<template>
  <div class="app-container">
    <el-form :inline="true" >
      <el-form-item label="姓名：">
        <el-autocomplete
          v-model="searchObj.name"
          :fetch-suggestions="querySearch"
          :trigger-on-focus="false"
          class="inline-input"
          value-key="name"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="头衔：">
        <el-select v-model="searchObj.level" clearable>
          <el-option value="1" label="高级讲师"></el-option>
          <el-option value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="入驻时间：">
        <el-date-picker
          v-model="searchObj.joinDateBegin"
          value-format="yyyy-MM-dd"
          placeholder="开始时间"
        />
      </el-form-item>
      <el-form-item label="-">
        <el-date-picker
          v-model="searchObj.joinDateEnd"
          placeholder="结束时间"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>

      <el-button type="danger" size="mini" @click="batchRemove()">批量删除</el-button>

    <el-table :data="list" border stripe  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40">
      </el-table-column>
      <el-table-column type="index" width="50">
        <template slot-scope="scope">
            {{(currentPage - 1) * pageSize + scope.$index+1}}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="80" />
      <el-table-column label="头衔" width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.level === 1" type="success" size="mini">高级讲师</el-tag>
          <el-tag v-if="scope.row.level === 2" size="mini">首席讲师</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="intro" label="简介" />
      <el-table-column prop="sort" label="排序" width="60" />
      <el-table-column prop="joinDate" label="入驻时间" width="160" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.id)">删除</el-button>
          <router-link :to="'/teacher/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

      <el-pagination
        align="right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher'
export default {
    data() {
      return {
        list: [],
        total: 0,
        currentPage: 1,
        pageSize: 5,
        searchObj: {}, //查询条件
        multipleSelection: []// 批量删除选中的记录列表
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        teacherApi.pageList(this.currentPage,this.pageSize,this.searchObj).then(res => {
          this.list = res.data.rows
          this.total = res.data.total
        })
      },
      removeById(id) {
        this.$confirm("是否删除这条记录？","提示",{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          teacherApi.removeById(id).then(res => {
              this.fetchData()
              this.$message.success(res.message)
          }).catch(error => {
             console.log('error', error)
            // 当取消时会进入catch语句:error = 'cancel'
            // 当后端服务抛出异常时：error = 'error'
            if (error === 'cancel') {
              this.$message.info('取消删除')
            }
          })
        })

      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.fetchData()
      },
      resetData() {
        this.searchObj = {}
        this.fetchData()
      },
      handleSelectionChange(val) {
        console.log(val)
        //选中对象的集合
        this.multipleSelection = val
      },
      //批量删除
      batchRemove() {
        if (this.multipleSelection.length == 0){
          this.$message.warning('请选择要删除的记录！')
          return;
        }
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示',{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() =>{
          let idList = []
          this.multipleSelection.forEach(item => {
                idList.push(item.id)
          })
          teacherApi.batchRemoves(idList).then(res => {
            this.$message.success(res.message)
            this.fetchData()
          })
        }).catch(error => {
          if (error === 'cancel') {
            this.$message.info('取消删除')
          }
        })
      },

      //名字模糊搜索
      querySearch(queryString, cb) {
        console.log("queryString",queryString)
        teacherApi.selectByKey(queryString).then(res => {
          cb(res.data.nameList)
        })
      }
    }
}
</script>
